package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    //地址列表
    @GetMapping("/address/list")
    public ResultInfo findList() {
        //调用servcie
        List<Address> addressList = addressService.findList();
        //返回结果

        return ResultInfo.success(addressList);
    }

    //新增地址
    @PostMapping("/address")
    public ResultInfo save(@RequestBody Address address) {// 1.接收请求参数
        addressService.save(address);   // 2.调用service新增
        return ResultInfo.success();  // 3.返回结果


    }

    //设置为默认地址
    @PutMapping("/address/default")
    public ResultInfo setDedault(@RequestBody Map<String, Long> param) {
        //1接收请求参数
        Long id = param.get("id");
        // 2调用service设置
        addressService.setDefault(id);
        // 3.返回结果
        return ResultInfo.success();
    }

    //去结算(查询 默认收货地址)
    @GetMapping("/address/default")
    public ResultInfo findDefault() {
        // 调用service查询
        Address address = addressService.findDefault();
        return ResultInfo.success(address);
    }



    //回显地址
    @GetMapping("/address/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        Address address = addressService.findById(id);
        return ResultInfo.success(address);
    }

    //修改地址
    @PutMapping("/address")
    public ResultInfo updateDefault(@RequestBody Address address) {
        addressService.updateDefault(address);
        return ResultInfo.success();

    }
//删除
    @DeleteMapping("address")
    public ResultInfo deleteIds(@RequestParam Long ids) {
        addressService.deleteIds(ids);
        return ResultInfo.success();
    }
}